/**
 * @Question.Title: LRU 缓存机制
 * @Question.No: 146
 * @Author: DQ
 * @Date: 2021-01-21 14:40:34
 * @Description: 
 */

//运用你所掌握的数据结构，设计和实现一个 LRU (最近最少使用) 缓存机制 。 
//
// 
// 
// 实现 LRUCache 类： 
//
// 
// LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 
// int get(int key) 如果关键字 key 存在于缓存中，则返回关键字的值，否则返回 -1 。 
// void put(int key, int value) 如果关键字已经存在，则变更其数据值；如果关键字不存在，则插入该组「关键字-值」。当缓存容量达到上
//限时，它应该在写入新数据之前删除最久未使用的数据值，从而为新的数据值留出空间。 
// 
//
// 
// 
// 
//
// 进阶：你是否可以在 O(1) 时间复杂度内完成这两种操作？ 
//
// 
//
// 示例： 
//
// 
//输入
//["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
//[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
//输出
//[null, null, null, 1, null, -1, null, -1, 3, 4]
//
//解释
//LRUCache lRUCache = new LRUCache(2);
//lRUCache.put(1, 1); // 缓存是 {1=1}
//lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
//lRUCache.get(1);    // 返回 1
//lRUCache.put(3, 3); // 该操作会使得关键字 2 作废，缓存是 {1=1, 3=3}
//lRUCache.get(2);    // 返回 -1 (未找到)
//lRUCache.put(4, 4); // 该操作会使得关键字 1 作废，缓存是 {4=4, 3=3}
//lRUCache.get(1);    // 返回 -1 (未找到)
//lRUCache.get(3);    // 返回 3
//lRUCache.get(4);    // 返回 4
// 
//
// 
//
// 提示： 
//
// 
// 1 <= capacity <= 3000 
// 0 <= key <= 3000 
// 0 <= value <= 104 
// 最多调用 3 * 104 次 get 和 put 
// 
// Related Topics 设计 
// 👍 1108 👎 0

package hot100.leetcode.editor.cn;

import java.util.*;

public class LruCache {

    public static void main(String[] args) {
        LRUCache lruCache = null;
        LruCache ll = new LruCache();
        String[] act = new String[]{"LRUCache","put","put","put","put","put","get","put","get","get","put","get",
                "put","put","put","get","put","get","get","get","get","put","put","get","get",
                "get","put","put","get","put","get","put","get","get","get","put","put","put",
                "get","put","get","get","put","put","get","put","put","put","put","get","put",
                "put","get","put","put","get","put","put","put","put","put","get","put","put",
                "get","put","get","get","get","put","get","get","put","put","put","put","get",
                "put","put","put","put","get","get","get","put","put","put","get","put","put",
                "put","get","put","put","put","get","get","get","put","put","put","put","get",
                "put","put","put","put","put","put","put"};
        StringBuilder nums1 = new StringBuilder("[10],[10,13],[3,17],[6,11],[10,5],[9,10],[13],[2,19],[2],[3],[5,25],[8],[9,22],[5,5],[1,30],[11],[9,12],[7],[5],[8],[9],[4,30],[9,3],[9],[10],[10],[6,14],[3,1],[3],[10,11],[8],[2,14],[1],[5],[4],[11,4],[12,24],[5,18],[13],[7,23],[8],[12],[3,27],[2,12],[5],[2,9],[13,4],[8,18],[1,7],[6],[9,29],[8,21],[5],[6,30],[1,12],[10],[4,15],[7,22],[11,26],[8,17],[9,29],[5],[3,4],[11,30],[12],[4,29],[3],[9],[6],[3,4],[1],[10],[3,29],[10,28],[1,20],[11,13],[3],[3,12],[3,8],[10,9],[3,26],[8],[7],[5],[13,17],[2,27],[11,15],[12],[9,19],[2,15],[3,16],[1],[12,17],[9,1],[6,19],[4],[5],[5],[8,1],[11,7],[5,2],[9,28],[1],[2,2],[7,4],[4,22],[7,24],[9,26],[13,28],[11,26]");
        String[] nums = nums1.substring(1,nums1.length()-1).split("],\\[");
        StringBuilder res = new StringBuilder("[");
        for (int i = 0; i < act.length; i++) {
            switch (act[i]){
                case "LRUCache":
                    lruCache = new LruCache().new LRUCache(Integer.parseInt(nums[i]));
                    res.append(" null");
                    break;
                case "get":
                    int val = lruCache.get(Integer.parseInt(nums[i]));
                    res.append(","+val);
//                    System.out.println("获取   key="+nums[i]+", value="+val);
                    break;
                case "put":
                    String[] temp = nums[i].split(",");

//                    System.out.println("放置   key="+temp[0]+", value"+temp[1]);
                    lruCache.put(Integer.parseInt(temp[0]),Integer.parseInt(temp[1]));
                    res.append(","+null);

                    break;
                default:

            }
        }
        res.append(" ]");
        System.out.println(res);


    }
        //leetcode submit region begin(Prohibit modification and deletion)
    class LRUCache {

        private int capactity;
        private Map<Integer,Entry> map;
        //双向循环链表
        private Entry twoWayList;
        final class Entry{
            Entry next,pre;
            int key,val;
            public Entry(int key,int val){
                this.key = key;
                this.val = val;
            }

            @Override
            public String toString() {
                return "(" + "key=" + key + ", val=" + val + ")";
            }
        }

        public LRUCache(int capacity) {
            this.capactity = capacity;
            map = new HashMap<>();
            
        }
        public int get(int key) {
            Entry res = map.get(key);
//            System.out.println("GET  key="+key+" ,val="+res);
            if(res==null) return -1;
            moveToHead(res);
//            showList(twoWayList);
            return res.val;
        }
        public void showList(Entry head){
            StringBuilder res = new StringBuilder("");
            if (head==null) {
                System.out.println("head=null");
                return;
            }
            res.append(String.format("(%d,%d)",head.key,head.val));
            Entry iter = head.next;

            while(iter!=head&&iter!=null){
                res.append(String.format(" -> (%d,%d)",iter.key,iter.val));
                iter = iter.next;
            }
            System.out.println(res);

        }
        public void moveToHead(Entry e){

            if(e==twoWayList){
                return;
            }
            if(e.next!=null){
                e.pre.next = e.next;
                e.next.pre = e.pre;
            }
            if(twoWayList==null){
                twoWayList = e;
            }

            e.next = twoWayList;
            e.pre = twoWayList.pre;
            twoWayList.pre = e;
            e.pre.next = e;
            twoWayList = e;

        }
        public void put(int key, int value) {
            Entry e = map.get(key);
            if(e!=null){
                e.val = value;
            }else {

                //没存满
                if (map.size() < capactity) {
                    e = new Entry(key, value);
                    map.put(key, e);
                } else {

                    //如果存满了，需要删除尾指针，这里直接使用尾指针，直接赋值
                    e = twoWayList.pre;
//                    System.out.printf("map.size()=%d, 缓存已满,放弃(%d,%d), 放入(%d,%d)\n", map.size(),tail.key,tail.val,e.key,e.val);
                    map.remove(e.key);
                    e.key = key;
                    e.val = value;
                    map.put(e.key,e);

                }
            }
            //将e移动到双向链表最前面，表示最近使用
            moveToHead(e);
//            showList(twoWayList);
        }
    }

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */
//leetcode submit region end(Prohibit modification and deletion)

}